package com.xhx.config;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;

@Aspect
@Component
public class DynamicDataSourceAspect {
    @Before("@annotation(dataSource)")
    public void changeDataSource(JoinPoint point,DataSource dataSource){
        String value = dataSource.value();
        System.out.println("value====="+value);
        if (value.equals("mysqlDataSource")){
            DataSourceType.setDataBaseType(DataSourceType.DataBaseType.mysqlDataSource);
        }else if (value.equals("mysql2DataSource")){
            DataSourceType.setDataBaseType(DataSourceType.DataBaseType.mysql2DataSource);
        }else {
            DataSourceType.setDataBaseType(DataSourceType.DataBaseType.mysqlDataSource);//默认使用主数据库
        }
    }

    @After("@annotation(DataSource)")
    public void afterSwitchDS(JoinPoint point){
        DataSourceContextHolder.clearDB();
    }
}
